package proyectoFC_2_3_3.koyicabuto.balloonOverlay;

import java.util.List;

import org.json.JSONException;

import proyectoFC_2_3_3.koyicabuto.R;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public abstract class BalloonItemizedOverlay<item> extends ItemizedOverlay{

	private MapView mapView;
	private BalloonOverlayView balloonView;
	private View clickRegion;
	private int viewOffset;
	final MapController mapController;
	private String bandera;

	public BalloonItemizedOverlay(Drawable defaultMarker, MapView mapView, String bandera) {
		super(defaultMarker);
		this.mapView = mapView;
		viewOffset = 0;
		mapController = mapView.getController();
		this.bandera = bandera;
	}
	
	public void setBalloonBottomOffset(int pixels) {
		viewOffset = pixels;
	}
	
	protected boolean onBalloonTap(int index) {
		return false;
	}

	@Override
	protected boolean onTap(int index) {
	
		boolean isRecycled;
		final int thisIndex;
		GeoPoint point;
		
		thisIndex = index;
		point = createItem(index).getPoint();
		
		if (balloonView == null) {
			try {
				
				balloonView = new BalloonOverlayView(mapView.getContext(), viewOffset, bandera);
				
				if(balloonView != null){
					clickRegion = (View) balloonView.findViewById(R.id.balloon_inner_layout);
					clickRegion.startAnimation(AnimationUtils.loadAnimation(mapView.getContext(), android.R.anim.fade_out));
					
					balloonView.startAnimation(AnimationUtils.loadAnimation(mapView.getContext(), android.R.anim.slide_out_right));
					balloonView.setVisibility(View.GONE);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			isRecycled = false;
		} else {
			isRecycled = true;
		}
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		if (mapOverlays.size() > 1) {
			hideOtherBalloons(mapOverlays);
		}

		balloonView.setData(createItem(index));

		MapView.LayoutParams params = new MapView.LayoutParams(
		LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
		MapView.LayoutParams.BOTTOM_CENTER);
		params.mode = MapView.LayoutParams.MODE_MAP;
		
		balloonView.startAnimation(AnimationUtils.loadAnimation(mapView.getContext(), android.R.anim.slide_in_left));
		balloonView.setVisibility(View.VISIBLE);
		
		if (isRecycled) {
			balloonView.setLayoutParams(params);
		} else {
			mapView.addView(balloonView, params);
		}	
		
		//Obtenemos el Zoom actual que tiene el Map y realizamos una animación para acercarnos al punto.
		for(int i=mapView.getZoomLevel(); i<10; i++){
			mapController.zoomIn();
		}
		mapController.animateTo(point);
		
		return true;
		}

		/**
		* Sets the visibility of this overlay’s balloon view to GONE.
		*/
		public void hideBalloon() {
			if (balloonView != null) {
				balloonView.startAnimation(AnimationUtils.loadAnimation(mapView.getContext(), android.R.anim.slide_out_right));
				balloonView.setVisibility(View.GONE);
			}
		}

		/**
		* Hides the balloon view for any other BalloonItemizedOverlay instances
		* that might be present on the MapView.
		*
		* @param overlays – list of overlays (including this) on the MapView.
		*/
		@SuppressWarnings("rawtypes")
		private void hideOtherBalloons(List<Overlay> overlays) {
		
			for (Overlay overlay : overlays) {
				if (overlay instanceof BalloonItemizedOverlay && overlay != this) {
					((BalloonItemizedOverlay) overlay).hideBalloon();
				}
			}
		
		}
}
